//
//  MZGPasswordView.m
//  Romomo
//
//  Created by puman on 2017/2/20.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "MZGPasswordView.h"

@implementation MZGPasswordView
{
    __block insertPasswordBlock _insertBlocks;

}
- (NSMutableArray *)countArray{
    if (_countArray == nil) {
        _countArray = [NSMutableArray array];
    }
    return _countArray;
}
- (NSMutableArray *)imageArray{
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 220, self.width, 220)];
        bottomView.backgroundColor = kCColor;
        [self addSubview:bottomView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bottomView.width, 16)];
        label.text = @"安全密码控件";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = kFont(10);
        label.backgroundColor = [UIColor whiteColor];
        label.textColor = COLORFROMCODE(0x9B9B9B, 1);
        [bottomView addSubview:label];
        NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"X",@"0",@"C",];
        CGFloat width = (self.width - 2) / 3;
        for (int i = 0; i < 12; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.frame = CGRectMake((width + 1) * (i % 3), label.bottom + 1 + 51 * (i / 3) , width, 50);
            button.backgroundColor = [UIColor whiteColor];
            [button setTitle:array[i] forState:(UIControlStateNormal)];
            [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
            button.titleLabel.font = kBFont(16);
            [bottomView addSubview:button];
            if (i  == 11) {
                [button addTarget:self action:@selector(delegate) forControlEvents:(UIControlEventTouchUpInside)];
            } else if (i != 9) {
                [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
            }
        }
        
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake((self.width - 280) / 2, bottomView.top - 180, 280, 160)];
        topView.backgroundColor = [UIColor whiteColor];
        topView.layer.masksToBounds = YES;
        topView.layer.cornerRadius = 3;
        topView.backgroundColor = [UIColor whiteColor];
        [self addSubview:topView];
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(0, 0, 60, 40);
        [button setTitle:@"取消" forState:(UIControlStateNormal)];
        [button setTitleColor:kBasicColor forState:(UIControlStateNormal)];
        [topView addSubview:button];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(button.right, 0, topView.width - button.width * 2, 40)];
        titleLabel.text = @"请输入支付密码";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = kFont(14);
        titleLabel.textColor = COLORFROMCODE(0x2C2C2C, 1);
        [topView addSubview:titleLabel];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, titleLabel.bottom, topView.width, 1)];
        lineView.backgroundColor = kCColor;
        [topView addSubview:lineView];
        
        UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, lineView.bottom, topView.width, 50)];
        priceLabel.textAlignment = NSTextAlignmentCenter;
        priceLabel.font = kFont(15);
        priceLabel.text = @"¥ 0.01";
        priceLabel.textColor = kBasicColor;
        [topView addSubview:priceLabel];
        
        UIView *mildView = [[UIView alloc] initWithFrame:CGRectMake((topView.width - 240) / 2.0, priceLabel.bottom, 240, 40)];
        mildView.backgroundColor = kCColor;
        mildView.layer.masksToBounds = YES;
        mildView.layer.cornerRadius = 3;
        [topView addSubview:mildView];
        
        CGFloat w = (240 - 7) / 6.0;
        for (int i = 0; i < 6; i++) {
            UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(1 + (w + 1) * i, 1, w, 38)];
            smallView.backgroundColor = [UIColor whiteColor];
            [mildView addSubview:smallView];
            
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake((w - 10) / 2.0, (38 - 10) / 2.0, 10, 10)];
            [self.imageArray addObject:imageV];
            imageV.backgroundColor = [UIColor blackColor];
            imageV.layer.masksToBounds = YES;
            imageV.layer.cornerRadius = 5;
            imageV.hidden = YES;
            [smallView addSubview:imageV];
        }
    }
    return self;
}
- (void)backPasswordString:(insertPasswordBlock)insertBlock{
    if (insertBlock) {
        _insertBlocks = insertBlock;
    }
}

- (void)buttonClick:(UIButton *)button{
    if (self.countArray.count == 6) {
        return;
    }
    NSString *string = [button titleForState:(UIControlStateNormal)];
    [self.countArray addObject:string];
    _insertBlocks([_countArray  componentsJoinedByString:@""]);
    [self changedImageViewBckGroundColor];
}
- (void)changedImageViewBckGroundColor{
    for (int i = 0; i < self.imageArray.count; i++) {
        UIImageView *imageV = self.imageArray[i];
        if (i < self.countArray.count) {
            imageV.hidden = NO;
        } else {
            imageV.hidden = YES;
        }
    }
}
- (void)delegate{
    if (self.countArray.count) {
        [self.countArray removeLastObject];
        [self changedImageViewBckGroundColor];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
